﻿using System;
using ITViet.Domains;
using ITViet.Services;
using ITViet.Web.App_Code;
using DevExpress.Web.ASPxGridView;

namespace ITViet.Web.Admin
{
  public partial class Groups : AdminBasePage
  {
    #region Properties
    private string m_innerError = string.Empty;

    internal override int RequiredPermission
    {
      get
      {
        return (int)FunctionList.Group;
      }
    }

    #endregion

    #region Methods

    private void LoadGroups()
    {
      gvGroup.DataSource = new GroupService().GetAll();
      gvGroup.DataBind();
    }

    private string DeleteGroup(int grpId)
    {
      try
      {
        TList<UserGroup> userOfGroup = new UserGroupService().GetByGroupId(grpId);
        if (userOfGroup != null && userOfGroup.Count > 0)
          return Resources.Messages.ERR0009;

        new GroupService().Delete(grpId);

      }
      catch (Exception ex)
      {
        WriteLog("Groups.aspx:Delete group", ex);
        return Resources.Messages.ERR0004;
      }
      return string.Empty;
    }

    private string SaveGroup(int grpId, string name, string note, bool isActive)
    {
      try
      {

        if (string.IsNullOrEmpty(name))
          return String.Format(Resources.Messages.ERR0007, Resources.Strings.Name);
        if (string.IsNullOrEmpty(note))
          note = null;
        GroupService grpService = new GroupService();
        Domains.Group group = null;
        if (grpId > 0)
        {
          group = grpService.GetById(grpId);
        }
        else
        {
          group = new Domains.Group();
        }
        if (group == null) return Resources.Messages.ERR0003;//Item not found
        group.Name = name;
        group.Description = note;
        group.IsActive = isActive;
        grpService.Save(group);
      }
      catch (Exception ex)
      {
        WriteLog("Groups.aspx:Save Group", ex);
        return Resources.Messages.ERR0006;
      }
      return string.Empty;
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsCallback && !IsPostBack)
      {
        LoadGroups();
      }
      if (gvGroup.IsCallback)
      {
        LoadGroups();
      }
    }

    protected void gvGroup_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
    {

    }

    protected void gvGroup_CustomJSProperties(object sender, ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }

    protected void gvGroup_RowDeleting(object sender, DevExpress.Web.Data.ASPxDataDeletingEventArgs e)
    {
      int grpId = 0;
      int.TryParse(e.Keys["Id"].ToString(), out grpId);
      m_innerError = DeleteGroup(grpId);
      e.Cancel = true;
      ASPxGridView gridControl = sender as ASPxGridView;
      gridControl.CancelEdit();
      LoadGroups();
    }

    protected void gvGroup_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
    {
      string name = (e.NewValues["Name"] != null) ? e.NewValues["Name"].ToString() : string.Empty;
      string note = (e.NewValues["Description"] != null) ? e.NewValues["Description"].ToString() : string.Empty;
      bool isActive = (e.NewValues["Description"] != null) ? Convert.ToBoolean(e.NewValues["IsActive"]) : true;
      m_innerError = SaveGroup(0, name, note, isActive);
      e.Cancel = true;
      ASPxGridView gridControl = sender as ASPxGridView;
      gridControl.CancelEdit();
      LoadGroups();
    }

    protected void gvGroup_RowUpdating(object sender, DevExpress.Web.Data.ASPxDataUpdatingEventArgs e)
    {
      int grpId = 0;
      int.TryParse(e.Keys["Id"].ToString(), out grpId);
      string name = (e.NewValues["Name"] != null) ? e.NewValues["Name"].ToString() : string.Empty;
      string note = (e.NewValues["Description"] != null) ? e.NewValues["Description"].ToString() : string.Empty;
      bool isActive = (e.NewValues["Description"] != null) ? Convert.ToBoolean(e.NewValues["IsActive"]) : true;
      m_innerError = SaveGroup(grpId, name, note, isActive);
      e.Cancel = true;
      ASPxGridView gridControl = sender as ASPxGridView;
      gridControl.CancelEdit();
      LoadGroups();
    }

    protected void gvGroup_CommandButtonInitialize(object sender, ASPxGridViewCommandButtonEventArgs e)
    {
      GridViewCommandColumnButton newButton = e.Column.NewButton;
      GridViewCommandColumnButton deleteButton = e.Column.DeleteButton;
      if (newButton != null && !CanModify) newButton.Visible = false;
      if (deleteButton != null && !CanDelete) deleteButton.Visible = false;

      if (!CanModify && e.Button.ButtonType == ColumnCommandButtonType.Edit)
      {
        e.Visible = false;
      }
    }

    protected void gvGroup_DataBound(object sender, EventArgs e)
    {
      if (!CanModify)
      {
        (gvGroup.Columns["Command"] as DevExpress.Web.ASPxGridView.GridViewCommandColumn).NewButton.Visible = false;
      }
    }
  }
}
